$(function(){
	$("#settlement").click(function(){
		$("#settlementcon").zxxbox({
			title : '订单确认',
			onshow : function(){
				var v = $('input[name="MID"]:checked').val();
				if(v == 0){
					if(!validation.checkAll()){
						window.location.hash="frm";
						return false;
					}
					return true;
				}
			}
		});
	 });
	siteSelect(["PROVINCE","CITY","COUNTY"],["","",""]);
	$Base.isLogin(location.href, getAddressList);
	getCart();
});

function getCart(){
	var url = '/json?action=QUERY_CART_BY_MEMBER_ACTION';
	$.post(url,{},function(data){
		if(data.success){
			var status = data.root.STATUS.rs[0];
			var cart = data.root.CART.rs[0].PRODUCT_LIST;
			var amount = data.root.CART.rs[0].AMOUNT;
			var count = data.root.CART.rs[0].ITEMS;
			$('#product_count').html(count);
			$('#amount').html('￥'+ amount);
			$('#pay_price').html('￥'+ amount);
			var listNode = $('#cart_list');
			listNode.html('');
			var productCount = 0;
			$.each(cart,function(i, obj){
				var divStr = [
					'<tr>',
						'<td>'+ obj.PRODUCT_NO +'</td>',
						 '<td class="shopping_table_img"><i><a href="../product/file.html?id='+obj.PRODUCT_ID+'"><img src="../upload/product/'+obj.PRODUCT_NO+'.jpg" /></a></i><p><a href="../product/file.html?id='+obj.PRODUCT_ID+'">'+ obj.PRODUCT_NAME +'</a></p></td>',
						'<td><span>￥'+ (obj.MEMBER_PRICE).toFixed(2) +'</span></td>',
						'<td>'+obj.QTY+'</td>',
						'<td>'+parseInt(obj.POINT)+'</td>',
						'<td><span>￥'+ (obj.AMOUNT).toFixed(2) +'</span></td>',
					'</tr>'
				].join('');
				listNode.append(divStr);
			});
		}
	},'json');
}

function getAddressList(){
	var url = '/json?action=QUERY_T_DELIVERY_ADDRESS_ACTION'
	$.post(url,{},function(data){
		if(data.success){
			var listNode = $('#address_list');
			var arr = data.root.T_DELIVERY_ADDRESS.rs;
			if(arr.length == 0){
				$('#new_address').show();
				$('input[name="MID"]').attr("checked","checked");
				return;
			}
			listNode.html('');
			$.each(arr, function(i,o){
				var divStr = [
					'<p><input name="MID" type="radio" value="'+o.DELIVERY_ADDRESS_ID+'" '+ (o.IS_DEFAULT == 1 ? 'checked' : '') +' onclick="showNewAddress();"/>',
					o.CONSIGNEE_NAME,
					'，',
					o.PROVINCE,
					'  ',
					o.CITY,
					'  ',
					o.COUNTY,
					'  ',
					o.DELIVERY_ADDRESS,
					',  ',
					o.ZIP_CODE,
					',  ',
					o.MOBILE,
					'  ',
					o.TELEPHONE,
					'</p>'
				].join('');
				listNode.append(divStr);
			});
		}
	},'json');
}

function showNewAddress(){
	var v = $('input[name="MID"]:checked').val();
	if(v == 0){
		$('#new_address').show();
	} else {
		$('#new_address').hide();
	}
}

var validation = {
	
	checkName: function(){
		var nameObj = $('#CONSIGNEE_NAME');
		$('#CONSIGNEE_NAME_TIP').html('');
		if($.trim(nameObj.val()).length == 0){
			$('#CONSIGNEE_NAME_TIP').html('收货人必填');
			return false;
		}
		return true;
	},
	
	checkPCC: function(){
		var province = $('#PROVINCE').val();
		var city = $('#CITY').val();
		var county = $('#COUNTY').val();
		$('#ADDRESS_TIP').html('');
		if(province.length == 0 || city.length == 0 || county.length ==0){
			$('#ADDRESS_TIP').html('请选择完整的地区信息');
			return false;
		}
		return true;
	}, 
	
	checkAddress: function(){
		var addressObj = $('#DELIVERY_ADDRESS');
		$('#DELIVERY_ADDRESS_TIP').html('');
		if($.trim(addressObj.val()).length == 0){
			$('#DELIVERY_ADDRESS_TIP').html('请填写真实的详细地址，方便送货人员能快速送货上门');
			return false;
		}
		return true;
	},
	
	checkContact: function(){
		var mobileObj = $('#MOBILE');
		var telephoneObj = $('#TELEPHONE');
		$('#MOBILE_TIP').html('');
		if($.trim(mobileObj.val()).length == 0 && $.trim(telephoneObj.val()).length == 0){
			$('#MOBILE_TIP').html('手机号码和固定电话必须填写一个，方便送货人员送货时联系您');
			return false;
		}
		return true;
	},
	
	checkAll: function(){
		return validation.checkName() && validation.checkPCC() && validation.checkAddress() && validation.checkContact();
	}
}

function subFrm(){
	var v = $('input[name="MID"]:checked').val();
	if(v == 0){
		if(!validation.checkAll()){
			return;
		}
	}
	
	var options = {
	    dataType: 'json',
    	iframe:	false,
	    success: function(data) {
	    	if(data.success){
	    		var id = data.root.ORDER_ID.rs[0];
	    		location.href="step3.html?id="+id;
	      	}else{
	      		alert(data.errors.errmsg);
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}

var count = 500;
function countWord(){
	var text = $.trim($('#MESSAGE').val());
	
	var e = e || event;
	var currKey = e.keyCode||e.which||e.charCode;
	if(currKey == 8 || currKey == 46){
		if(count - text.length <= 0){
			$('#num').html('0');
		}
		else{
			$('#num').html(count - text.length);
		}
		return true;
	}
	
	if(count - text.length <= 0){
		$('#num').html('0');
		return false;
	}
	$('#num').html(count - text.length);
	return true;
}
